//
//  RootTabBarViewController.m
//  daxiagmu
//
//  Created by brother on 16/2/22.
//  Copyright © 2016年 brother. All rights reserved.
//

#import "RootTabBarViewController.h"

@interface RootTabBarViewController ()

@property (nonatomic, weak) UIView *myView;

@property (nonatomic, weak) UIButton *selectBtn;

@end

@implementation RootTabBarViewController

//tabBar隐藏和动画的实现
- (void)setIsHide:(BOOL)isHide {
    _isHide = isHide;
    if (isHide == YES) {
            self.myView.y = self.view.height;
            self.myView.hidden = YES;
    }else {
        [UIView animateWithDuration:0.3f animations:^{
            self.myView.y = self.view.height - self.myView.height;
        }];
        self.myView.hidden = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.hidden = YES;
    UIView *myView = [[UIView alloc] init];
    myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabBar_bg"]];
    myView.frame = self.tabBar.frame;
    [self.view addSubview:myView];
    self.myView = myView;
    // 创建5个button
    [self creatCustomBtn];
    // Do any additional setup after loading the view.
}

- (void)creatCustomBtn {
    //循环创建4个按钮 并添加到myView视图
    for (int i = 0; i < 4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *normalStr = [NSString stringWithFormat:@"tabBar_%d_normal",i+1];
        
        NSString *pressedStr = [NSString stringWithFormat:@"tabBar_%d_press",i+1];
        
        [btn setImage:[UIImage imageNamed:pressedStr] forState:UIControlStateNormal];
        
        [btn setImage:[UIImage imageNamed:normalStr] forState:UIControlStateSelected];
        
        CGFloat buttonW = self.myView.frame.size.width * 0.25;
        CGFloat buttonH = self.myView.frame.size.height;
        CGFloat buttonX = i * buttonW;
        CGFloat buttonY = 0;
        
        [btn setFrame:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
        
        btn.tag = i;
        
        [btn addTarget:self action:@selector(changeController:) forControlEvents:UIControlEventTouchUpInside];
        [self.myView addSubview:btn];
        // 默认选中第1个控制器
        if (i == 0) {
            [self changeController:btn];
        }
    }
}

- (void)changeController:(UIButton *)button {
//    NSLog(@"%@",button);
    self.selectBtn.selected = NO;  // 之前的先不选中
    
    button.selected = YES; // 当前的选中
    
    self.selectBtn = button; // 保存当前的选中
    
    self.selectedIndex  = button.tag;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
